﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Admin.Business.Sitemap
{
    public static class Index
    {
        public static Adams.Admin.DataTransfer.Sitemap.Index.PageLoad PageLoad()
        {
            return DataAccess.Sitemap.Index.PageLoad();
        }

        public static void Update(Adams.Admin.DataTransfer.Sitemap.Index.UpdateInput input)
        {
            DataAccess.Sitemap.Index.Update(input);
        }

        public static DataTransfer.FormOutput SaveNode(DataTransfer.Sitemap.Index.NodeFormInput input)
        {
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                var nodeinfo = DataAccess.Sitemap.Index.GetNodeInfo(input.Url, input.ID.ToInt());

                if (input.ID.ToInt() > 0)
                {
                    if (!Shared.Str.IsEqual(input.Url, nodeinfo.Url))
                    {
                        if (nodeinfo.Exists)
                        {
                            output.InvalidFields.Add(new DataTransfer.InvalidField()
                            {
                                Name = "Url",
                                Code = 4
                            });
                        }
                    }
                }
                else
                {
                    if (nodeinfo.Exists)
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "Url",
                            Code = 4
                        });
                    }
                }

                if (output.IsValid)
                {
                    output.ID = DataAccess.Sitemap.Index.SaveNode(input);
                    output.IsSaved = true;
                }
            }

            return output;
        }

        public static Adams.Admin.DataTransfer.Sitemap.Index.GetNode GetNode(Adams.Admin.DataTransfer.Sitemap.Index.GetNodeInput input)
        {
            return DataAccess.Sitemap.Index.GetNode(input);
        }

        public static void DeleteNode(int nodeId)
        {
            DataAccess.Sitemap.Index.DeleteNode(nodeId);
        }
    }
}
